/**
 * Copyright 2018 asiainfo Inc.
 **/
package com.myz.service.feign.web;

import com.myz.service.feign.feign.ScheduleService;
import com.myz.service.feign.feign.ScheduleUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @author maoyz on 2018/9/7
 * @version: v1.0
 */
@RequestMapping("/feign")
@RestController
public class FeignController {

    @Autowired
    private ScheduleService scheduleService;

    @Autowired
    private ScheduleUploadService scheduleUploadService;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String hello() {
        String index = scheduleService.index();
        return index;
    }

    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public String get(HttpServletRequest request) {
        System.out.println(request.getHeader("Authorization"));
        return scheduleService.get(1);
    }

    @RequestMapping(value = "/header", method = RequestMethod.GET)
    public String header(HttpServletRequest request) {
        System.out.println(request.getHeader("Authorization"));
        return scheduleService.header("feign-secret", "maoyz");
    }

    @RequestMapping(value = "/error0", method = RequestMethod.GET)
    public String error(@RequestParam(value = "username") String username, HttpServletRequest request) {
        return scheduleService.error(username);
    }

    @RequestMapping(value = "/url404", method = RequestMethod.GET)
    public String url404() {
        return scheduleService.url404();
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String upload(HttpServletRequest request, @RequestPart(value = "file") MultipartFile file) {
        System.out.println(request.getHeader("Authorization"));
        System.out.println(file.getOriginalFilename());
        return scheduleUploadService.handleFileUpload(file);
    }

}
